home *** CD-ROM | disk | FTP | other *** search
- // shell for reading pict's into a buffered offscreen window.
- // The guts of this is in the window creation function:
- // DoCreateWindow( PicHandle thePicture ), this file also
- // contains a simple main event loop.
- //
- // Nick Thompson, Summer 1994
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
-
-
- #include <menus.h>
- #include <PictUtil.h>
- #include <QDOffScreen.h>
- #include "OffscreenShell.h"
- #include "CompressImageTest.h"
-
- static OSType gCodec = '????';
- static short gDepth = 0 ;
-
-
- main()
- {
- InitToolbox() ;
-
- MainEventLoop();
- }
-
-
-
- void checkForQuickTime()
- {
- long version;
-
- if (Gestalt( gestaltQuickTime, &version ) != noErr)
- {
- ParamText( "\pQuickTime not installed. Please install, then try again.", "\p", "\p", "\p" );
- Alert( 129, nil );
- ExitToShell();
- }
- }
-
-
- void InitToolbox(void)
- {
- OSErr retCode;
- long gestResponse;
- Handle menuBar = nil;
- EventRecord event;
- short count;
-
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs((long)nil);
- InitCursor();
-
- // initialize application globals
-
- gQuitFlag = false;
-
-
- menuBar = GetNewMBar(128); // Read menus into menu bar, MBAR res id is 128
-
- if ( menuBar == nil )
- ExitToShell(); // if we dont have it then quit - your app
- // needs a dialog here
-
- SetMenuBar(menuBar); // Install menus
- DisposHandle(menuBar);
-
- AddResMenu(GetMHandle(mApple), 'DRVR'); // Add DA names to Apple menu, ID 128
-
- DrawMenuBar();
-
- checkForQuickTime() ;
- }
-
-
- void MainEventLoop()
- {
- EventRecord event;
- WindowPtr window;
- short thePart;
- Rect screenRect;
- Point aPoint = {100, 100};
- GWorldPtr theNewWorld ;
- PixMapHandle offPixMap ;
- GrafPtr oldPort ;
-
- while( !gQuitFlag )
- {
- if (WaitNextEvent( everyEvent, &event, 0, nil ))
- {
- AdjustMenus() ;
-
- switch (event.what) {
- case mouseDown:
-
- thePart = FindWindow( event.where, &window );
-
- switch( thePart ) {
- case inMenuBar:
- HandleMenuCommand(MenuSelect(event.where));
- break;
-
- case inDrag:
-
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( window, event.where, &screenRect );
- break ;
-
- case inContent:
-
- if (window != FrontWindow())
- SelectWindow( window );
- break ;
-
- case inGoAway:
- if (TrackGoAway( window, event.where )) {
- DisposeWindow ( window );
- }
- break ;
-
- default:
- break ;
- }
- break ;
-
-
- case updateEvt:
-
- window = (WindowPtr)event.message;
- GetPort(&oldPort ) ;
- SetPort( window );
-
- BeginUpdate( window );
-
- // get the GWorld from the window refcon
- theNewWorld = (GWorldPtr)GetWRefCon ( window );
- offPixMap = GetGWorldPixMap( theNewWorld ) ;
- (void) LockPixels( offPixMap ) ;
- CopyBits( &((GrafPtr)theNewWorld)->portBits,
- &window->portBits,
- &window->portRect,
- &window->portRect,
- srcCopy,
- nil ) ;
- (void) UnlockPixels( offPixMap ) ;
-
- EndUpdate( window );
- SetPort( oldPort ) ;
- break ;
-
- case keyDown:
- case autoKey:
- HandleKeyPress(&event);
- break;
-
- case diskEvt:
- if ( HiWrd(event.message) != noErr )
- (void) DIBadMount(aPoint, event.message);
- break;
-
- case osEvt:
- case activateEvt:
- break;
-
-
- }
- }
- }
- }
-
-
- void HandleKeyPress(EventRecord *event)
- {
- char key;
-
- key = event->message & charCodeMask;
-
- // just check to see if we want to quit...
-
- if ( event->modifiers & cmdKey ) { /* Command key down? */
- HandleMenuCommand(MenuKey(key));
- }
- }
-
-
- void HandleMenuCommand(long menuResult)
- {
- short menuID;
- short menuItem;
- Str255 daName;
- DialogPtr theDialog ;
- short itemHit ;
- SFTypeList myTypes = { 'PICT' } ;
- FSSpec theFSSpec ;
- PicHandle thePicture ;
- OSErr err ;
- short theRef ;
-
- StandardFileReply theSFReply ;
-
- menuID = HiWrd(menuResult);
- menuItem = LoWrd(menuResult);
- switch ( menuID ) {
- case mApple:
- switch ( menuItem ) {
- case iAbout:
- theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
- do {
- ModalDialog ( nil, &itemHit );
- } while( itemHit != ok ) ;
- DisposDialog ( theDialog );
- break;
-
- default:
- GetItem(GetMHandle(mApple), menuItem, daName);
- (void) OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch ( menuItem ) {
- case iOpen:
- // Get the file name to open
- StandardGetFilePreview( nil, 2, myTypes, &theSFReply ) ;
-
- // did the user cancel?
- if(!theSFReply.sfGood)
- break ;
-
- // open the file
- err = FSpOpenDF( &theSFReply.sfFile, fsRdPerm, &theRef ) ;
-
- if( err != noErr )
- break ; // should handle this properly
-
- thePicture = DoReadPICT( theRef, &err ) ;
-
- if( err != noErr )
- break ; // should handle this properly
-
- // display the contents
- err = DoCreateWindow( thePicture ) ;
-
- break ;
-
- case iClose:
- DisposeWindow ( FrontWindow() );
- break ;
-
- case iSave:
- DoSaveAs( FrontWindow() ) ;
- break ;
-
- case iQuit:
- gQuitFlag = true;
-
- while( FrontWindow())
- DisposeWindow ( FrontWindow() );
-
- break;
- }
- break;
-
- case mPalette:
- switch ( menuItem ) {
- case iUsePictPalette :
- // toggle the check mark and the global boolean
- gUsePictPalette = !gUsePictPalette ;
- CheckItem ( GetMHandle ( mPalette ), iUsePictPalette, gUsePictPalette );
- break ;
- }
- break ;
- }
- HiliteMenu(0); // Unhighlight whatever MenuSelect or MenuKey hilited
- }
-
- void AdjustMenus( void )
- {
- WindowPtr theWindow ;
- theWindow = FrontWindow() ;
- if( theWindow != nil ) {
- EnableItem ( GetMHandle ( mFile ), iClose );
- }
- else {
- DisableItem ( GetMHandle ( mFile ), iClose );
- }
- // make sure the check marks are correct
- CheckItem ( GetMHandle ( mPalette ), iUsePictPalette, gUsePictPalette );
- }
-
- PicHandle DoReadPICT( short theRef, OSErr *theErr )
- {
- long theFileSize ;
- PicHandle thePicture ;
-
- // pict files have a 512 byte header at the front - we dont care about this
- // we can find the size of the pict by subtracting 512 bytes from the length
- // of the file. We then want to resize the handle to that and read the data
- // into the resized handle.
-
- if(( *theErr = GetEOF( theRef, &theFileSize )) != noErr ) {
- FSClose( theRef ) ;
- return nil ;
- }
-
- if(( *theErr = SetFPos( theRef, fsFromStart, 512)) != noErr ) {
- FSClose( theRef ) ;
- return nil ;
- }
-
- theFileSize -= 512 ;
-
- thePicture = (PicHandle)NewHandle( theFileSize ) ;
- if( thePicture == nil ) {
- FSClose( theRef ) ;
- *theErr = MemError() ;
- return nil ; // what ever the mem manager error was
- }
-
- HLock( (Handle)thePicture ) ;
- *theErr = FSRead( theRef, &theFileSize, (Ptr)*thePicture ) ;
- HUnlock( (Handle)thePicture ) ;
-
- if( *theErr != noErr ) {
- FSClose( theRef ) ;
- return nil ;
- }
-
- return thePicture ;
- }
-
-
- pascal void myStdPix( PixMapPtr src, Rect *srcRect, MatrixRecordPtr matrix,
- short mode, RgnHandle mask, PixMapPtr matte,
- Rect *matteRect, short flags )
- {
- ImageDescriptionHandle desc;
- Ptr data;
- long bufferSize;
-
- GetCompressedPixMapInfo( src, &desc, &data, &bufferSize, nil, nil );
- gCodec = (**desc).cType;
- gDepth = (**desc).depth;
- }
-
- pascal void myTextProc( short byteCount, Ptr textBuf, Point numer, Point denom )
- {
- }
-
- pascal void myLineProc( Point newPt )
- {
- }
-
- pascal void myRectProc( GrafVerb verb, Rect *r )
- {
- }
-
- pascal void myRRectProc( GrafVerb verb, Rect *r, short ovalWidth, short ovalHeight )
- {
- }
-
- pascal void myOvalProc( GrafVerb verb, Rect *r )
- {
- }
-
- pascal void myArcProc( GrafVerb verb, Rect *r, short startAngle, short arcAngle )
- {
- }
-
- pascal void myPolyProc( GrafVerb verb, PolyHandle poly )
- {
- }
-
- pascal void myRgnProc( GrafVerb verb, RgnHandle rgn )
- {
- }
-
- pascal void myBitsProc( BitMap *bitPtr, Rect *srcRect, Rect *dstRect,
- short mode, RgnHandle maskRgn )
- {
- }
-
-
- void doBottleneckTest( PicHandle picture )
- {
- int i;
- CQDProcs bottlenecks;
-
- /* Define our own bottlenecks to do nothing. */
- SetStdCProcs( &bottlenecks );
-
- bottlenecks.textProc = (QDTextUPP)myTextProc;
- bottlenecks.lineProc = (QDLineUPP)myLineProc;
- bottlenecks.rectProc = (QDRectUPP)myRectProc;
- bottlenecks.rRectProc = (QDRRectUPP)myRRectProc;
- bottlenecks.ovalProc = (QDOvalUPP)myOvalProc;
- bottlenecks.arcProc = (QDArcUPP)myArcProc;
- bottlenecks.polyProc = (QDPolyUPP)myPolyProc;
- bottlenecks.rgnProc = (QDRgnUPP)myRgnProc;
- bottlenecks.bitsProc = (QDBitsUPP)myBitsProc;
- bottlenecks.newProc1 = (UniversalProcPtr)myStdPix; /* pixProc */
-
- /* Load & draw pictures from resource. */
- picture = (PicHandle)Get1IndResource( 'PICT', i + 1 );
-
- /* Install our custom bottlenecks to intercept any compressed images. */
- (*(qd.thePort)).grafProcs = (QDProcs *)&bottlenecks;
- DrawPicture( picture, &((**picture).picFrame) );
-
- /* Switch back to the default procs. */
- (*(qd.thePort)).grafProcs = 0L;
- }
-
-
- OSErr DoCreateWindow( PicHandle thePicture )
- {
-
- Rect theRect ;
- OSErr theErr ;
- GWorldPtr theNewWorld ;
- CGrafPtr savedPort ;
- GWorldPtr savedGWorld ;
- WindowPtr theWindow ;
- GDHandle oldDevice ;
-
- PictInfo thePictInfo ;
- PaletteHandle thePictPalette = nil ;
- CTabHandle thePictCTab = nil ;
-
- // make an offscreen environment and image the pict into this
- // Make a window the size of the pict
- // store a reference to the GWorld in the Refcon of the window
- // invalidate the window content area.
-
- theRect.top = (**thePicture).picFrame.top ;
- theRect.left = (**thePicture).picFrame.left ;
- theRect.bottom = (**thePicture).picFrame.bottom ;
- theRect.right = (**thePicture).picFrame.right ;
-
- doBottleneckTest( thePicture ) ;
- thePictInfo.depth = gDepth ;
-
- theErr = NewGWorld( &theNewWorld, thePictInfo.depth, &theRect, thePictCTab, nil, 0L ) ;
-
- if( theErr != noErr )
- return theErr ;
-
- // save the world
- GetGWorld( &savedPort, &oldDevice ) ;
- SetGWorld( theNewWorld, nil ) ;
-
-
- RGBForeColor( &kRGBBlack ) ; // ensure the fg and bg colors are
- RGBBackColor( &kRGBWhite ) ; // as anticipated
- EraseRect( &theRect ) ; // clear the area for the pict
- PenMode( srcCopy ) ; // ensure the t/f mode is as expected
-
- // render the image into the offscreen buffer
- DrawPicture( thePicture, &theRect ) ;
-
- SetGWorld( savedPort, oldDevice ) ;
-
- // create the window
- OffsetRect( &theRect, gStaggerPos.h, gStaggerPos.v) ;
- gStaggerPos.h += 16 ;
- gStaggerPos.v += 16 ; // heh - should roll these around, but you wont
- // create more than a couple of windows, will you :-)
-
- theWindow = NewCWindow( nil, &theRect, "\pplayTime", true,
- documentProc, (WindowPtr)-1, true, (long)theNewWorld );
-
- // and if we set up the palette earlier assign it to the window
- if( thePictPalette != nil ) {
- SetPalette ( theWindow, thePictPalette, true );
- }
-
- ActivatePalette ( theWindow );
-
- // make sure it is visible
- ShowWindow( theWindow ) ;
-
- SetGWorld( (CGrafPtr)theWindow, nil ) ;
-
- // invalidate the content region of the window - we don't do any drawing to it here.
- InvalRect ( &theRect );
-
- SetGWorld( savedPort, oldDevice ) ;
- }